package com.kc.web.controller.business;

import com.kc.business.domain.BizGoodsLease;
import com.kc.business.service.IBizGoodsLeaseService;
import com.kc.business.service.ImageService;
import com.kc.common.annotation.Log;
import com.kc.common.annotation.RepeatSubmit;
import com.kc.common.core.controller.BaseController;
import com.kc.common.core.domain.AjaxResult;
import com.kc.common.core.page.TableDataInfo;
import com.kc.common.enums.BusinessType;
import com.kc.common.utils.ImagesUtils;
import com.kc.common.utils.StringUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.Arrays;
import java.util.Map;

/**
 * 租赁商品管理
 */
@RestController
@RequestMapping("/business/goodsLease")
public class BizGoodsLeaseController extends BaseController {
    @Resource
    private IBizGoodsLeaseService bizGoodsLeaseService;
    @Resource
    private ImageService imageService;
    /**
     * 新增租赁商品
     */
    @PreAuthorize("@ss.hasPermi('business:goodsLease:add')")
    @Log(title = "租赁商品管理", businessType = BusinessType.INSERT)
    @PostMapping
    @RepeatSubmit
    public AjaxResult add(@Validated @RequestBody BizGoodsLease bizGoodsLease) {
        //图片数组转字符串逗号分隔
        bizGoodsLease.setImages(StringUtils.join(bizGoodsLease.getGoodsImgs(), ","));
        //处理列表图片宽高
        bizGoodsLease.setImageAttr(imageService.getImageSizeToJson(bizGoodsLease.getGoodsImgs()));
        //获取商品详情图片
        String[] contents = bizGoodsLease.getContent().split(",");
        bizGoodsLease.setContentInfo(imageService.getImageSizeToJson(contents));
        //缩略图宽高
        try {
            Map<String,Integer> map = ImagesUtils.getImgSize(bizGoodsLease.getThumb());
            bizGoodsLease.setImgWidth(map.get("width"));
            bizGoodsLease.setImgHeight(map.get("height"));
        } catch (IOException e) {
            e.printStackTrace();
        }
        bizGoodsLeaseService.add(bizGoodsLease);
        return toAjax(1);
    }

    /**
     * 租赁商品编辑
     * @param bizGoodsLease
     * @return
     */
    @PreAuthorize("@ss.hasPermi('business:goodsLease:edit')")
    @Log(title = "租赁商品管理", businessType = BusinessType.UPDATE)
    @PutMapping
    @RepeatSubmit
    public AjaxResult edit(@Validated @RequestBody BizGoodsLease bizGoodsLease) {
        //图片数组转字符串逗号分隔
        bizGoodsLease.setImages(StringUtils.join(bizGoodsLease.getGoodsImgs(), ","));
        //处理列表图片宽高
        bizGoodsLease.setImageAttr(imageService.getImageSizeToJson(bizGoodsLease.getGoodsImgs()));
        //缩略图宽高
        try {
            Map<String,Integer> map = ImagesUtils.getImgSize(bizGoodsLease.getThumb());
            bizGoodsLease.setImgWidth(map.get("width"));
            bizGoodsLease.setImgHeight(map.get("height"));
        } catch (IOException e) {
            e.printStackTrace();
        }
        bizGoodsLeaseService.edit(bizGoodsLease);
        return toAjax(1);
    }

    /**
     * 租赁商品删除
     * @param id
     * @return
     */
    @PreAuthorize("@ss.hasPermi('business:goodsLease:remove')")
    @Log(title = "租赁商品管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable("id") Integer id) {
        BizGoodsLease bizGoodsLease = new BizGoodsLease();
        bizGoodsLease.setId(id);
        bizGoodsLease = bizGoodsLeaseService.getInfo(bizGoodsLease);
        bizGoodsLease.setIsDeleted(1);
        return toAjax(bizGoodsLeaseService.remove(bizGoodsLease));
    }

    /**
     * 商品列表
     * @param bizGoodsLease
     * @return
     */
    @PreAuthorize("@ss.hasPermi('business:goodsLease:getList')")
    @GetMapping
    public TableDataInfo getList(BizGoodsLease bizGoodsLease) {
        startPage();
        return getDataTable(bizGoodsLeaseService.getList(bizGoodsLease));
    }

}
